package com.handinfo.android.core.input;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWGestureListener;
import com.handinfo.android.uicontrols.DWGestureRecognition;
import com.handinfo.android.utils.Tools;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class DWInputFactory implements OnTouchListener, DWGestureListener
{
	//检测手势的类
//	private GestureDetector mGesture;

	private DWIInputHandler mHandler;

	private DWGestureRecognition mGR = null;

	private DWDPad mDPad;

	public DWInputFactory()
	{
//		mGesture = new GestureDetector(this);
		mDPad = new DWDPad();
		mGR = new DWGestureRecognition(this);
	}

	public DWInputFactory(DWIInputHandler handler)
	{
//		mGesture = new GestureDetector(this);
		mDPad = new DWDPad();
		mHandler = handler;
		mGR = new DWGestureRecognition(this);
	}

	//	 触屏监听

	public void createDPad()
	{
		mDPad = new DWDPad();
	}

	public void destroyDPad()
	{
		mDPad = null;
	}

	public boolean onTouch(View v, MotionEvent event)
	{
		if (DWGameManager.getInstance().getInputView().isFocused()) {
			DWGameManager.getInstance().getGameView().requestFocus();
		}
		if (mDPad != null) {
			mDPad.onTouch(event);
		}
		mGR.onTouch(event);
		return true;
	}

	public void setHandler(DWIInputHandler handler)
	{
		this.mHandler = handler;
	}

	public DWDPad getDPad()
	{
		return mDPad;
	}

	public void drawDPad(DWGraphics g)
	{
		if (mDPad != null) {
			mDPad.draw(g);
		}
	}

	/**
	 * 按下
	 * @param point
	 */
	public boolean onDown(PointF point)
	{
//		Tools.debugPrintln("=========按下=========");
		if (mHandler != null) {
			try {
				mHandler.doDown(point);
			}
			catch (Exception e) {
				Tools.debug(e);
			}
		}
		return false;
	}

	/**
	 * 点击
	 * @param point
	 */
	public boolean onClick(PointF point)
	{
//		Tools.debugPrintln("=========点击=========");
		if (mHandler != null) {
			try {
				mHandler.doClick(point);
			}
			catch (Exception e) {
				Tools.debug(e);
			}
		}
		return false;
	}

	/**
	 * 双击
	 * @param point
	 */
	public boolean onDoubleClick(PointF point)
	{
//		Tools.debugPrintln("=========双击=========");
		if (mHandler != null) {
			try {
				mHandler.doDoubleClick(point);
			}
			catch (Exception e) {
				Tools.debug(e);
			}
		}
		return false;
	}

	/**
	 * 长按
	 * @param point
	 */
	public boolean onLongPress(PointF point)
	{
//		Tools.debugPrintln("=========长按=========");
		if (mHandler != null) {
			try {
				mHandler.doLongPress(point);
			}
			catch (Exception e) {
				Tools.debug(e);
			}
		}
		return false;
	}

	/**
	 * 点击移动
	 * @param start
	 * @param end
	 * @param distanceX 位移
	 * @param distanceY
	 */
	public boolean onScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
//		Tools.debugPrintln("=========点击移动=========");
		if (mHandler != null) {
			try {
				mHandler.doScroll(start, end, distanceX, distanceY);
			}
			catch (Exception e) {
				Tools.debug(e);
			}
		}
		return false;
	}

	/**
	 * 长按移动
	 * @param start
	 * @param end
	 * @param distanceX 位移
	 * @param distanceY
	 */
	public boolean onLongScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
//		Tools.debugPrintln("=========长按移动=========");
		if (mHandler != null) {
			try {
				mHandler.doLongScroll(start, end, distanceX, distanceY);
			}
			catch (Exception e) {
				Tools.debug(e);
			}
		}
		return false;
	}

	/**
	 * 按下移动后抬起
	 * @param start
	 * @param end
	 * @param velocityX
	 * @param velocityY
	 */
	public boolean onFling(PointF start, PointF end, float velocityX, float velocityY)
	{
//		Tools.debugPrintln("=========按下移动抬起=========");
		if (mHandler != null) {
			try {
				mHandler.doFling(start, end, velocityX, velocityY);
			}
			catch (Exception e) {
				Tools.debug(e);
			}
		}
		return false;
	}

	/**
	 * 长按移动后抬起
	 * @param start
	 * @param end
	 * @param velocityX
	 * @param velocityY
	 */
	public boolean onDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
//		Tools.debugPrintln("=========长按移动抬起=========");
		if (mHandler != null) {
			try {
				mHandler.doDrag(start, end, velocityX, velocityY);
			}
			catch (Exception e) {
				Tools.debug(e);
			}
		}
		return false;
	}

	public boolean onTouch(MotionEvent event, PointF point)
	{
		if (mHandler != null) {
			try {
				mHandler.onTouch(event, point);
			}
			catch (Exception e) {
				Tools.debug(e);
			}
		}
		return false;
	}

//	//按下
//	public boolean onDown(MotionEvent e)
//	{
////		Tools.debugPrintln("onDown!");
//		return true; 
//	}
//	//先短暂按下，短按不滑动
//	public void onShowPress(MotionEvent e) {
//		
//	}
//
//	//短暂按下抬起
//	public boolean onSingleTapUp(MotionEvent e) {
//		Tools.debugPrintln("onSingleTapUp!");
//		if (mHandler != null)
//			return mHandler.doClick(new PointF(e.getX(), e.getY()));
//		return true;
//	}
//
//	//先短暂按下，然后滑动     
//	//e1保存按下点坐标 e2保存抬起点坐标   
//	//distance对于上次时间X的偏移量
//	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
////		Tools.debugPrintln("onScroll!");
//		if (mHandler != null)
//			return mHandler.doScroll(new PointF(e1.getX(), e1.getY()), new PointF(e2.getX(), e2.getY()), distanceX, distanceY);
//		return true;
//	}
//
//	//长按不滑动
//	public void onLongPress(MotionEvent e) {
////		Tools.debugPrintln("onLongPress!");
//		if (mHandler != null)
//			mHandler.doLongPress(new PointF(e.getX(), e.getY()));
//	}
//
//	//先短暂按下，然后滑动，最后抬起
//	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//		
////		Tools.debugPrintln("onFling!");
//		if (mHandler != null)
//			return mHandler.doFling(new PointF(e1.getX(), e1.getY()), new PointF(e2.getX(), e2.getY()), velocityX, velocityY);
//		return true;
//	}
//
//	public boolean onSingleTapConfirmed(MotionEvent e) {
//		Tools.debugPrintln("onSingleTapConfirmed");
//		return false;
//	}
//
//	public boolean onDoubleTap(MotionEvent e) {
//		Tools.debugPrintln("onDoubleTap");
//		return false;
//	}
//
//	public boolean onDoubleTapEvent(MotionEvent e) {
//		Tools.debugPrintln("onDoubleTapEvent");
//		return false;
//	}

}
